package com.njlime.cms.repository.sys;

import com.njlime.cms.entities.MaterialsVoiceGroup;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-7-18.
 */
@Repository
public class MaterialVoiceGroupDaoImpl extends BaseDaoImpl<MaterialsVoiceGroup> {
    public void editGroup(String id, String name, int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("UPDATE t_materials_voice_group set name = ?");

        sb.append(" where id = ? and unit_id = ?");
        arrList.add(name);
        arrList.add(id);
        arrList.add(unitId);
        updateJdbc(sb.toString(), arrList);
    }
    public void deleteGroup(String id,  int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("delete from  t_materials_voice_group ");

        sb.append(" where id = ? and unit_id = ?");
        arrList.add(id);
        arrList.add(unitId);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 获取列表
     *
     * @param unitId
     * @return
     */
    public List<MaterialsVoiceGroup> list(int unitId) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.*  from t_materials_voice_group t ");

        sb.append("where t.unit_id = ?  and del = 0");

        arrList.add(unitId);

        return findList(sb.toString(), arrList);
    }



    /**
     * 得到总人数
     * @param unitId
     * @return
     */
    public Map getSumMember(int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select  sum(1) count from t_materials_voice t ");

        sb.append( "  where   unit_id = ? and del = 0 ");

        arrList.add(unitId);
        List<Map<String, Object>>  list =  getListJdbc(sb.toString(), arrList);
        if(list != null && list.size()>0){
            return list.get(0);
        }
        return null ;
    }



    /**
     * 减少分组下的数量
     * @param imageGroupId
     */
    public void downCount(Integer imageGroupId,int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("UPDATE t_materials_voice_group set count =if(count = 0,0,count -1)  ");
        sb.append(" where id= ?  and unit_id = ?");

        arrList.add(imageGroupId);
        arrList.add(unitId);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 增加分组下的数量
     * @param imageGroupId
     */
    public void upCount(Integer imageGroupId,int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("UPDATE t_materials_voice_group set count = count + 1");
        sb.append(" where id= ?  and unit_id = ?");

        arrList.add(imageGroupId);
        arrList.add(unitId);
        updateJdbc(sb.toString(), arrList);
    }

}
